﻿using Order;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace OrderForm
{
    public partial class MainForm : Form
    {
        OrderService service;
        List<Order.Order> Orders { get; set; }
        public MainForm()
        {
            InitializeComponent();
        }

        private async void Query_Click(object sender, EventArgs e)
        {
            RefreshOrder(await service.InquiryOrdersAsync(info.Text, (OrderService.InquiryType)searchType.SelectedItem));
        }
        private async void RefreshOrder(List<Order.Order> orders)
        {
            if (orders == null)
                orders = await service.InquiryOrdersAsync(null, OrderService.InquiryType.All);
            Orders = orders;
            orderView.DataSource = Orders;

        }
        private void RefreshDetail(List<OrderDetail> details)
        {
            detailView.DataSource = details;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            service = new();
            searchType.DataSource = new List<OrderService.InquiryType> {
                OrderService.InquiryType.OrderID,
                OrderService.InquiryType.CustomerName,
                OrderService.InquiryType.CommodityName,
                OrderService.InquiryType.Total,
                OrderService.InquiryType.All
            };
            orderView.AutoGenerateColumns = true;
            detailView.AutoGenerateColumns = true;
            RefreshOrder(null);
        }

        private void OrderView_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            RefreshDetail(Orders[e.RowIndex].OrderDetails);
        }

        private async void Add_Click(object sender, EventArgs e)
        {
            Order.Order order = new();
            if (new ModifyOrder(order).ShowDialog(this) == DialogResult.OK)
                try
                {
                    await service.AddOrder(order);
                    RefreshOrder(null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning,
                        MessageBoxDefaultButton.Button1);
                    throw;
                }
        }

        private async void Delete_Click(object sender, EventArgs e)
        {
            if (orderView.SelectedRows.Count > 0 && orderView.SelectedRows[0].DataBoundItem is Order.Order order)
            {
                try
                {
                    await service.DeleteOrder(order.OrderID);
                    RefreshOrder(null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK,
                        MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    throw;
                }
            }
        }

        private async void Modify_Click(object sender, EventArgs e)
        {
            if (orderView.SelectedRows.Count > 0 && orderView.SelectedRows?[0].DataBoundItem is Order.Order order)
            {
                order = order.Clone() as Order.Order;
                var id = order.OrderID;
                if (new ModifyOrder(order).ShowDialog(this) == DialogResult.OK)
                    try
                    {
                        await service.ModifyOrder(id, order);
                        await service.AddOrder(order);
                        RefreshOrder(null);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning,
                            MessageBoxDefaultButton.Button1);
                        throw;
                    }
            }
        }

        private void Export_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                using var stream = saveFileDialog1.OpenFile();
                service.Export(stream);
            }
        }

        private async void Import_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                using var stream = openFileDialog1.OpenFile();
                await service.Import(stream);
                RefreshOrder(null);
            }
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            service.Dispose();
        }
    }
}
